﻿using Microsoft.Extensions.DependencyInjection;
using Modules.Project.Entitys;
using PetaPoco;
using PmSoft;
using PmSoft.ApplicationContext;
using PmSoft.DBContext;
using PmSoft.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Modules.Project.Repositories
{
    [UseDI(ServiceLifetime.Scoped)]
    public class ProjectCheckinPushLogRepository : Repository<ProjectCheckinPushLog, DefaultDbContext>
    {
        public ProjectCheckinPushLogRepository(DefaultDbContext dbContext, IApplicationContext applicationContext) : base(dbContext, applicationContext)
        {
        }

        /// <summary>
        /// 查没有上月没有更新的项目月报表的项目ID集合
        /// </summary>
        /// <param name="month"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public IEnumerable<int> GetProjectIdsInNoUpdate(int month, int limit = 100)
        {
            Sql sql = Sql.Builder
               .Select("a.project_id")
               .From("report_project_month AS a")
               .Where("a.month=@0 AND NOT EXISTS (SELECT  b.project_id FROM project_checkin_push_log AS b WHERE b.project_id=a.project_id AND b.month=@0)", month)
               .Append($"LIMIT {limit}");
            return DbContext.FetchFirstColumn<int>(sql);
        }
    }
}
